#include <stdio.h>
#include <stdlib.h>
#define N 10


void average();
void print_menu();
void getData(int[], int);
void printmarks(int[]);
double calcavg(int[], int);
void printupavg(int[], double, int);
void countlowavg(int[], double, int);


void main(){
	int key, cont =1;
	printf("Welcome to Exrercise 5\n\n");
	print_menu();
	while  (cont != 0){
		scanf("%d", &key);
		switch (key){
			case 1:{
				system("cls");
				square();
				print_menu();
				break;
				   }
			case 2:{
				system("cls");
				average();
				print_menu();
				break;
				   }
			default:{
				cont = 0;
				break;
					}
		}
	}
}


void average(){
	int marks[N];
	double avg;
	printf("Welcome to Marks Application\n");
	getData(marks,N);
	avg = calcavg(marks,N);
	printf("The marks average is: %.2lf\n", avg);
	printupavg(marks, avg, N);
	countlowavg(marks, avg, N);
}
int square(){
	int num, res;
	printf("Enter an Integer to calculate:\n");
	scanf("%d", & num);
	if(num < 0){
		printf("Invalid input");
		return;
	}
	res = sqrtsummary(num);
	printf("%d\n", res);
	return 0;
}
int sqrtsummary(int num){
	int res = 0;
	if (num ==0){
		return 0;
	}
	res = num * num;
	res +=sqrtsummary(num-1);
	return res;
}
void print_menu(){
	printf("Welcome to Exercise 5\n");
	printf("1- Square summary of num\n");
	printf("2- Student marks calculator\n");
	printf("0- Exit\n");
}
void getData(int marks[],int lenght){
   int i;
   printf("Enter Marks for each student (0-100)\n");
   for(i=0;i<lenght;i++){
   	   while (marks[i] < 0 || marks[i] >100){
   		   scanf("%d",&marks[i]);
   		   if (marks[i] < 0 || marks[i] > 100){
   			   printf("Invalid input, (0 - 100)\n");
   		   }
   	   }
   }
   return;
  }
void printmarks(int marks[]){
	int i;
	for(i = 0; i < 10; i++){
		printf("Mark, %d\n", marks[i]);
	}
	return;
}
double calcavg(int marks[], int length){
	int i;
 	double avg = 0;
	for(i = 0; i < length; i++){
		avg += marks[i];
	}
	avg = avg / length;
	return avg;
}
void printupavg(int marks[], double avg, int length){
	int i;
	printf("Students with marks upper than average:\n");
	for(i = 0; i < length; i++){
		if(marks[i] >= avg){
			printf("The mark of %d student is: %d\n", i, marks[i]);
		}
	}
	return;
}
void countlowavg(int marks[], double avg, int length){
	int i;
	int res = 0;
	printf("Lower Average:\n");
	for(i = 0; i < length; i++){
		if(marks[i] < avg){
			res +=1;
		}
	}
	printf("%d student is/are under the grade average\n", res);
	return;
}